<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @selection-change="selectionChange"
            >
              <template slot="menuLeft">
                <el-button type="primary" icon="el-icon-edit"  size="small" @click="addReturn">创建退货单</el-button>
              </template>
            </avue-crud>
        </basic-container>
    </div>
</template>
<!-- 供应订单记录 -->
<script>
    import {
        fetchList,
        addReturnOrderByOfferOrder
    } from '@/api/supply/offerorder'
    import {
        tableOption
    } from '@/const/crud/pds/supply/offerorder'
    import {getStore, setStore} from "@/util/store";
    export default {
        name: 'order',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                pmId: '',
                list:[]
            }
        },
        created() {
            this.pmId = getStore({name: "pmId"});
        },
        computed: {
        },
        methods: {
            selectionChange(list){
              for(var i in list){
                if(list[i].status != 3 || list[i].shipStatus != 4){
                  this.$refs.crud.toggleSelection();
                  this.$message.error("请选择已中标并且发货状态为已接收的");
                  return false;
                }
                if(list[i].supplierName != list[0].supplierName){
                  this.$refs.crud.toggleSelection();
                  this.$message.error("请选择同一个供应商");
                  return false;
                }
              }
              //
              this.list = list;
            },
            addReturn(){
                let params = {};
                params.pmId = this.pmId;
                params.otherAmount = 0;
                params.list = this.list;
                if(params.list.length){
                  addReturnOrderByOfferOrder(params).then(response => {
                    this.$message.success("新增成功");
                    this.getList(this.page);
                    this.$refs.crud.toggleSelection();
                  })
                }else{
                  this.$message.error("请选择订单");
                }
            },
            getList(page, params={}) {
                this.tableLoading = true
                params.pmId = this.pmId;
                params.current = page.currentPage;
                params.size = page.pageSize;
                fetchList(params).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
},
        }
    }
</script>

<style lang="scss" scoped>
</style>
